﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;

namespace Jy123.Web.Friend
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class FriendClassList : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            int userID;
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                context.Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);
            string delCID = context.Request["cID"];
            if (!string.IsNullOrEmpty(delCID))
            {
                string msg = string.Empty;
                try
                {
                    new Jy123.BLL.User_FriendClass().Delete(Convert.ToInt32(delCID));
                    //这里还要批量更新分类ID为将被删除的ID所在的所有好友，换成默认分类
                    msg = "删除成功,ok";
                }
                catch (Exception ex)
                {
                    msg = ex + ",error";
                }                
                context.Response.ContentType = "text/plain";
                context.Response.Write(msg);
            }
            else
            {
                context.Response.ContentType = "text/plain";            
                context.Response.Write(FrdClsList(context, userID.ToString()));//写出分组列表 
            }
            
        }
        /// <summary>
        /// 取得用户的好友分组列表及分组人数
        /// </summary>
        /// <param name="context"></param>
        /// <param name="strUserID">用户ID</param>
        /// <returns>字符串</returns>
        private string FrdClsList(HttpContext context,string strUserID)
        {
            int userID = Convert.ToInt32(strUserID);
            string cacheFrdCls = context.Cache["user_friendclasslist_li"].ToString();
            StringBuilder sbFrdCls = new StringBuilder();
            List<Jy123.Model.User_FriendClass> listFrdCls = new Jy123.BLL.User_FriendClass().GetModelList("FUser='"+strUserID+"' order by ID ASC");
            foreach (Jy123.Model.User_FriendClass frdC in listFrdCls)
            {
                string strFrdCls = cacheFrdCls;
                strFrdCls = strFrdCls.Replace("$CID$",frdC.ID.ToString());
                strFrdCls = strFrdCls.Replace("$CName$", frdC.CName);
                
                List<int?> count;
                new BLL.User.Friend().GetFriendList(1, int.MaxValue, userID, frdC.ID, out count);
                int? frdCount = count[0];
                strFrdCls = strFrdCls.Replace("$FrdCount$", frdCount.ToString());
                sbFrdCls.Append(strFrdCls);
            }
            sbFrdCls.Append(@"
            <script type=""text/javascript"">
                $(function(){
                    $("".fdlist img"").click(function(){
                        myAjax('/Friend/FriendClassList.ashx','cID='+$(this).attr(""fclassid""));
	                    $(this).parent().fadeOut();				
                    })
                })
            </script>");
            return sbFrdCls.ToString();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
